package com.android.questions.ui.adapter;

import android.content.Context;
import android.util.TypedValue;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.android.base.util.ActivityClickUtil;
import com.android.questions.R;
import com.android.questions.config.LoginConfig;
import com.android.questions.util.AnimUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

/**
 * FileName: HomeRunAdapter
 * Author: admin
 * Date: 2021/11/5 14:07
 * Description:
 */
public class HomeRunAdapter extends BaseQuickAdapter<String, BaseViewHolder> {

    private OnItemClickLister clickLister;
    private OnGuideLister guideLister;
    private int rightIndex = -1;

    public HomeRunAdapter() {
        super(R.layout.item_home_run);
    }

    @Override
    protected int getDefItemViewType(int position) {
        return position;
    }

    @Override
    protected void convert(@NonNull BaseViewHolder baseViewHolder, String data) {
        Context context = getContext();
        final int pos = getItemPosition(data);
        baseViewHolder.setText(R.id.tvAnswer, data);
        TextView tvAnswer = baseViewHolder.getView(R.id.tvAnswer);
        tvAnswer.setBackgroundResource(R.drawable.icon_home_run);
//        tvAnswer.setTextColor(context.getResources().getColor(R.color.Color_874345));
        tvAnswer.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
        tvAnswer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                DQLog.e("answer option: " + tvAnswer.getText().toString());
//                DQLog.e("tvAnswer clicked");
                if (ActivityClickUtil.getInstance().isFastClick()) {
                    //防止用户快速点击
                    return;
                }
//                DQLog.e("tvAnswer onItemClick");
                if (clickLister != null) {
                    clickLister.onItemClick(pos, tvAnswer);
                }
            }
        });

        if (LoginConfig.getGuideIndex() == 0 && rightIndex == -1) {
            guideLister.onGuideView(pos, tvAnswer);
        }

        if (rightIndex == pos) {
            AnimUtil.receiveAnimM(context, tvAnswer);
        } else {
            tvAnswer.clearAnimation();
        }
    }

    public void setOnItemClickLister(OnItemClickLister lister) {
        this.clickLister = lister;
    }

    public void setGuideLister(OnGuideLister lister) {
        this.guideLister = lister;
    }

    public void updateAnswerResult(TextView tvAnswer, boolean right) {
        if (right) {
            tvAnswer.setBackgroundResource(R.drawable.icon_home_run_true);
        } else {
            tvAnswer.setBackgroundResource(R.drawable.icon_home_run_false);
        }
    }

    public void setRightIndex(int index) {
        this.rightIndex = index;
    }

    public interface OnItemClickLister {
        void onItemClick(int position, TextView tvAnswer);
    }

    public interface OnGuideLister {
        void onGuideView(int position, TextView tvAnswer);
    }
}
